package com.litxuf.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.litxuf.FinalInformation;
import com.litxuf.logic.core.ManagerFactory;
import com.litxuf.logic.member.dao.model.School;
import com.litxuf.logic.member.manager.MemberManager;
import com.litxuf.util.DateUtil;

public class MemberLoginServlet extends HttpServlet {
	Logger logger = Logger.getLogger(this.getClass());

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		MemberManager manager = ManagerFactory.getFactory().getMemberManager();

		if (username == null || pwd == null) {
			request.setAttribute("Error", "用户名密码不能为空");
			logger.info("登录->" + username + " 用户名密码不能为空");
			request.getRequestDispatcher("/login.jsp").forward(request,
					response);
			return;
		}
		School user = manager.findMemberByMemberNameAndPwd(username
				.toLowerCase(), pwd);
		logger.info("===>用户名:" + username);
		logger.info("===>密码:" + pwd);

		if (user == null) {
			request.setAttribute("Error", "用户名或密码错误");
			logger.info("登录->" + username + " 用户名或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request,
					response);
			return;
		}  

		 
 
		String lastlogintime=user.getLastlogintime();
		request.getSession().setAttribute("lastlogintime", lastlogintime);
		user.setLastlogintime(DateUtil.currDateStr("yyyy-MM-dd HH:mm:ss"));
		manager.updateMember(user);

		 
		 request.getSession()
			.setAttribute(FinalInformation.UserFrontLogin, user);

		try {
			logger.info(" -->开始跳转到用户中心首页");
			response.sendRedirect(request.getContextPath()
					+ "/member/index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
